package top.heliming.youyou.item.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.heliming.youyou.item.pojo.Category;
import top.heliming.youyou.item.service.CategoryService;

/**
 * description: //商品
 *
 * @author: heliming
 * @date:2019/05/26 下午 9:36
 */
@RestController
@RequestMapping("category")
public class CategoryController {

  @Autowired
  private CategoryService categoryService;

  /**
   * 根据父节点查询商品类目
   * @param pid
   * @return
   */
  @GetMapping("list")
  public ResponseEntity<List<Category>> queryByParentId(
      @RequestParam(value = "pid", defaultValue = "0") Long pid) {
    List<Category> list = this.categoryService.queryListByParent(pid);
    if (list == null || list.size() < 1) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    return ResponseEntity.ok(list);
  }

  /**
   * 通过品牌id查询商品分类
   * @param bid
   * @return
   */
  @GetMapping("bid/{bid}")
  public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid") Long bid) {
    List<Category> list = this.categoryService.queryByBrandId(bid);
    if (list == null || list.size() < 1) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    return ResponseEntity.ok(list);
  }

  /**
   * 根据商品分类id查询名称
   * @param ids 要查询的分类id集合
   * @return 多个名称的集合
   */
  @GetMapping("names")
  public ResponseEntity<List<String>> queryNameByIds(@RequestParam("ids") List<Long> ids){
    List<String > list = this.categoryService.queryNameByIds(ids);
    if (list == null || list.size() < 1) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    return ResponseEntity.ok(list);
  }
  /**
   * 根据3级分类id，查询1~3级的分类
   * @param id
   * @return
   */
  @GetMapping("all/level")
  public ResponseEntity<List<Category>> queryAllByCid3(@RequestParam("id") Long id){
    List<Category> list = this.categoryService.queryAllByCid3(id);
    if (list == null || list.size() < 1) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    return ResponseEntity.ok(list);
  }
}
